﻿using BLL;
using System;
using System.Web.UI.WebControls;

namespace Thksoft.Module.ParameterSettings
{
    public partial class EntrustSettings : BaseUI.BasePage
    {
        private Qs_EntrustBLL entrustBLL = new Qs_EntrustBLL();
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnQuery_Click(sender, e);
            }
        }

        protected void DELETE_CLICK(object sender, EventArgs e)
        {
            var keys = DELETE_CHECK_IDS.Value;
            var sql = "DELETE FROM QS_ENTRUST WHERE N_ENTRUST_ID IN(" + keys + ")";
            if (entrustBLL.NoQuery(sql) > 0)
            {
                MessageBox.Show(this, "删除成功。");
            }
            else
            {
                MessageBox.Show(this, "删除失败。");
            }
            BindData(false);
        }

        protected void ENTRUST_CLICK(object sender, EventArgs e)
        {
            var dataStr = CHECK_ID.Value.Split('|');
            var entity = entrustBLL.Find(dataStr[0]);
            if (entity == null)
            {
                MessageBox.Show(this, "没有找到委托");
            }
            if (dataStr[1] == "1")
            {
                entity.C_Entrust_state = "0";
            }
            else
            {
                entity.C_Entrust_state = "1";
            }

            if (entrustBLL.Update(entity))
            {
                MessageBox.Show(this, "修改成功。");
            }
            else
            {
                MessageBox.Show(this, "修改失败。");
            }
            BindData(false);
        }

        #region 代码块

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            var searchkey = txtKey.Text.TrimEnd();
            if (searchkey == "")
            {
                if (lognUser.USER_LOGINNAME == "SUPER")
                {
                    ViewState["WHERE"] = null;
                }
                else
                {
                    var cmyIds = companyBLL.GetCompanyFatherAndChildByID(CurrentUserCompanyID);
                    if (cmyIds == "")
                    {
                        MessageBox.Show(Page, "当前登录人公司已经不存在！");
                        return;
                    }
                    ViewState["WHERE"] = "T1.COMPANY_ID IN(" + cmyIds + ")";
                }
                BindData(true);
            }
            else
            {
                var where = "";
                if (lognUser.USER_LOGINNAME == "SUPER")
                {
                    if (rdbYes.Checked)
                    {
                        where = "T2.USER_NAME LIKE '%" + searchkey + "%'";
                    }
                    else
                    {
                        where = "T3.USER_NAME LIKE '%" + searchkey + "%'";
                    }
                }
                else
                {
                    where = "T1.COMPANY_ID IN(" + companyBLL.GetCompanyFatherAndChildByID(CurrentUserCompanyID) + ")";
                    if (rdbYes.Checked)
                    {
                        where = " T2.USER_NAME LIKE '%" + searchkey + "%'";
                    }
                    else
                    {
                        where = " T3.USER_NAME LIKE '%" + searchkey + "%'";
                    }
                }
                ViewState["WHERE"] = where;
                BindData(true);
            }
        }

        protected void BindData(bool iffirst)
        {
            var sql = @"SELECT
	T1.n_entrust_id,
    T1.n_principal,
    T1.n_user_id,
    to_char(T1.d_ntrust_start_date, 'yyyy-mm-dd HH24:MI:SS') d_ntrust_start_date,
    T1.c_entrust_state,
    T1.n_entrust_remarks,
    to_char(T1.d_ntrust_operating_time, 'yyyy-mm-dd HH24:MI:SS') d_ntrust_operating_time,
    T1.company_id,
    to_char(T1.d_ntrust_enddata, 'yyyy-mm-dd HH24:MI:SS') d_ntrust_enddata,
	T2.USER_NAME AS ENTRUST_USER_NAME,
    T2.USER_ID AS ENTRUST_USER_ID,
	T3.USER_NAME AS USER_NAME,
    T3.USER_ID AS USER_ID
FROM QS_ENTRUST T1
LEFT JOIN SYS_USER T2
ON T1.N_PRINCIPAL = T2.USER_ID
LEFT JOIN SYS_USER T3
ON T1.N_USER_ID = T3.USER_ID";
            var where = ViewState["WHERE"] == null ? null : (string)ViewState["WHERE"];
            if (where != null)
            {
                sql += " WHERE " + where;
            }
            var orderBy = (string)ViewState["SortOrder"];
            if (orderBy != null)
            {
                sql += " ORDER BY " + orderBy + " " + (string)ViewState["OrderDire"];
            }
            if (iffirst)
            {
                PageIng.CurrentPage = 1;
                PageIng.RowsCount = entrustBLL.SqlCount(sql);
            }
            var dt = entrustBLL.PageQuery(sql, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage);
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.Visible = false;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        #endregion 代码块

        /// <summary>
        /// 标题列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            //查询
            btnQuery_Click(sender, e);
        }
    }
}